package org.ourgrid.worker.business.requester;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.RequesterIF;
import org.ourgrid.common.internal.response.LoggerResponseTO;
import org.ourgrid.worker.business.controller.WorkerController;
import org.ourgrid.worker.business.messages.ControlMessages;
import org.ourgrid.worker.request.StopWorkerRequestTO;

public class StopWorkerRequester implements RequesterIF<StopWorkerRequestTO> {

	public List<IResponseTO> execute(StopWorkerRequestTO request) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		if (request.isStopSenderPublicKeyValid() && request.canComponentBeUsed()){
			
			WorkerController.getInstance().interruptWorkingAndCancelPreparingAllocation(responses, true);
			
			LoggerResponseTO loggerResponseTO = new LoggerResponseTO();
			loggerResponseTO.setMessage(ControlMessages.getSuccessfullyShutdownWorkerMessage());
			loggerResponseTO.setType(LoggerResponseTO.INFO);
			responses.add(loggerResponseTO);
			
		}
		if(!request.isStopSenderPublicKeyValid()){
			responses.add(new LoggerResponseTO(
					ControlMessages.getUnknownEntityTryingToStopWorkerMessage(
							request.getStopSenderPublicKey()),LoggerResponseTO.WARN));
		}
		
		return responses;
	}
}
